#include "masterview.h"
#include "ui_masterview.h"
#include "idatabase.h"
#include "QDebug"
MasterView::MasterView(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MasterView)
{
    ui->setupUi(this);
    //设置边框 将窗口设置为无边框窗口
    this->setWindowFlag(Qt::FramelessWindowHint);

    goLoginView();

    IDatabase::getInstance();
}

MasterView::~MasterView()
{
    delete ui;
}

void MasterView::goLoginView()
{
    qDebug()<<"goLoginView";
    //创建登录界面
    loginView = new LoginView(this);
    //添加界面
    pushWidgetToStackView(loginView);

    //绑定切换页面信号
    connect(loginView,SIGNAL(loginSuccess()),this,SLOT(goWelcomeView()));
}

void MasterView::goWelcomeView()
{
    qDebug()<<"goWelcomeView";
    //创建界面
    welcomeView = new WelcomeViem(this);

    pushWidgetToStackView(welcomeView);

    connect(welcomeView,SIGNAL(goDoctorView()),this,SLOT(goDoctorView()));
    connect(welcomeView,SIGNAL(goPatientView()),this,SLOT(goPatientView()));
    connect(welcomeView,SIGNAL(goDepartmentView()),this,SLOT(goDepartmentView()));
}

void MasterView::goDoctorView()
{
    qDebug()<<"goDoctorView";
    //创建界面
    doctorView = new DoctorView(this);

    pushWidgetToStackView(doctorView);
}

void MasterView::goDepartmentView()
{
    qDebug()<<"goDepartmentView";
    //创建界面
    departmentView = new DepartmentView(this);

    pushWidgetToStackView(departmentView);
}

void MasterView::goPatientView()
{
    qDebug()<<"goPatientView";
    //创建界面
    patientView = new PatientView(this);

    pushWidgetToStackView(patientView);

    connect(patientView,SIGNAL(goPatientEditView(int)),this,SLOT(goPatientEditView(int)));
}

void MasterView::goPatientEditView(int rowNo)
{
    qDebug()<<"goPatientEditView";
    //创建界面
    patientEditView = new PatientEditView(this,rowNo);

    pushWidgetToStackView(patientEditView);

    connect(patientEditView,SIGNAL(goPreviousView()),this,SLOT(goPreviousView()));
}

void MasterView::goPreviousView()
{
    int count = ui->stackedWidget->count();

    if(count>1){
        ui->stackedWidget->setCurrentIndex(count - 2);
        ui->labelTitle->setText(ui->stackedWidget->windowTitle());

        QWidget *widget = ui->stackedWidget->widget(count-1);
        ui->stackedWidget->removeWidget(widget);
        delete widget;
    }
}

void MasterView::pushWidgetToStackView(QWidget *widget)
{

    //添加界面
    ui->stackedWidget->addWidget(widget);

    //获取页面个数
    int count = ui->stackedWidget->count();
    ui->stackedWidget->setCurrentIndex(count-1);//总是显示最新加入的view
    ui->labelTitle->setText(widget->windowTitle());
}

void MasterView::on_btBack_clicked()
{
    goPreviousView();
}


void MasterView::on_stackedWidget_currentChanged(int arg1)
{
    // int count = ui->stackedWidget->count();
    // if(count>1){
    //     ui->btBack->setEnabled(true);
    // }
    // else{
    //     ui->btBack->setEnabled(false);
    //}

    //通过title控制按钮是否可用
    QString title = ui->stackedWidget->currentWidget()->windowTitle();
    if(title == "登录"){
        ui->btBack->setEnabled(false);
        ui->btLogout->setEnabled(false);
    }
    else if(title == "欢迎"){
        ui->btBack->setEnabled(false);
        ui->btLogout->setEnabled(true);
    }else{
        ui->btBack->setEnabled(true);
        ui->btLogout->setEnabled(false);
    }
}


void MasterView::on_btLogout_clicked()
{
    goLoginView();
}

